package com.qiall.fileutil;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;

import com.alibaba.excel.event.AnalysisEventListener;

import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;

import com.alibaba.excel.write.metadata.WriteSheet;

import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>ClassName：EasyExcel工具类</p >
 * <p>Description：</p >
 * <p>Date：2021/9/1</p >
 */
@Slf4j
public class EasyExcelUtil {


    //-------------------------------------------------------------- 读取文件解析监听类 start ----------------------------------------------------

    /**
     * <p>ClassName：ExcelListener</p >
     * <p>Description：读取文件解析监听类，此类供外部实例化使用需要设置为静态类</p >
     * <p>Date：2021/9/2</p >
     */
    public static class ExcelListener<T> extends AnalysisEventListener<T> {

        /**
         * <p>存放读取后的数据</p >
         *
         * @date 2021/9/2 0:10
         */
        public List<T> datas = new ArrayList<>();

        /**
         * <p>读取数据，一条一条读取</p >
         *
         * @date 2021/9/2 0:15
         */
        @Override
        public void invoke(T t, AnalysisContext analysisContext) {
            datas.add(t);
        }

        /**
         * <p>解析完毕之后执行</p >
         *
         * @date 2021/9/2 0:06
         */
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            log.info("读取数据条数：{}条！", datas.size());
        }

        public List<T> getDatas() {
            return this.datas;
        }

    }
    //-------------------------------------------------------------- 读取文件解析监听类 end ----------------------------------------------------


    //-------------------------------------------------------------- 导出excel表格，设置自适应列宽配置类 start ----------------------------------------------------

    /**
     * <p>ClassName：Custemhandler</p >
     * <p>Description：设置自适应列宽配置类</p >
     * <p>Date：2021/10/14</p >
     */
    public static class Custemhandler extends AbstractColumnWidthStyleStrategy {

        private static final int MAX_COLUMN_WIDTH = 255;
        //因为在自动列宽的过程中，有些设置地方让列宽显得紧凑，所以做出了个判断
        private static final int COLUMN_WIDTH = 20;
        private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);
    }
    //-------------------------------------------------------------- 导出excel表格，设置自适应列宽配置类 end -----------------------------------------------------


    /**
     * <p> 读取Excel文件返回数据集合，不包含表头，默认读取第一个sheet数据 </p >
     *
     * @param inputStream   输入流
     * @param tClass        数据映射类
     * @param excelListener 读取监听类
     * @return List 结果集
     * @date 2021/9/2 0:17
     */
    public static <T> List<T> readExcel(InputStream inputStream, Class<T> tClass, ExcelListener<T> excelListener) {
        if (inputStream==null || tClass==null||excelListener==null) {
            return null;
        }
        ExcelReaderBuilder read = EasyExcel.read(inputStream, tClass, excelListener);
        read.sheet().doRead();
        return excelListener.getDatas();
    }

    /**
     * <p> 读取Excel文件返回数据集合，不包含表头，读取第x个sheet数据，不设置sheet就读取全部 </p >
     *
     * @param inputStream   输入流
     * @param tClass        数据映射类
     * @param excelListener 读取监听类
     * @return List 结果集
     * @date 2021/9/2 0:17
     */
    public static <T> List<T> readExcel(InputStream inputStream, Integer sheetNo, Class<T> tClass, ExcelListener<T> excelListener) {
        if ((inputStream==null|| tClass==null|| excelListener==null)) {
            return null;
        }
        ExcelReaderBuilder read = EasyExcel.read(inputStream, tClass, excelListener);
        if (sheetNo==null) {
            read.sheet(sheetNo).doRead();
        } else {
            ExcelReader excelReader = read.build();
            excelReader.readAll();
            excelReader.finish();
        }
        return excelListener.getDatas();
    }

    /**
     * <p>不带模板输出数据到Excel，数据传输类属性用 @ExcelProperty("") 标注</p >
     *
     * @param response 响应对象
     * @param tClass   输出格式
     * @param datas    输出的数据
     * @date 2021/9/2 0:32
     * @return：
     */
    public static <T> void writeEasyExcel(HttpServletResponse response, Class<T> tClass, List<T> datas, String fileName) throws IOException {
        if (response==null || tClass==null) {
            return;
        }

        if (StringUtils.isBlank(fileName)) {
            fileName = "excel.xlsx";
        } else {
            if (!fileName.contains(".xlsx")) {
                fileName = fileName + ".xlsx";
            }
        }
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
        EasyExcel.write(response.getOutputStream(), tClass)
                .registerWriteHandler(new Custemhandler())
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("sheet")
                .doWrite(datas);
    }

    /**
     * <p>带模板输出数据到Excel，数据传输类属性用 @ExcelProperty("") 标注</p >
     *
     * @param outputStream 输出流
     * @param tClass       输出格式
     * @param datas        输出的数据
     * @date 2021/9/2 0:32
     * @return：
     */
    public static <T> void writeExcel(InputStream inputStream, OutputStream outputStream, Class<T> tClass, List<T> datas) {
        if (outputStream==null || tClass==null) {
            return;
        }
//        EasyExcel.write(outputStream, tClass).withTemplate(inputStream).sheet("sheet").doWrite(datas);
        EasyExcel.write(outputStream, tClass).excelType(ExcelTypeEnum.XLSX).withTemplate(inputStream).sheet("sheet").doFill(datas);
    }


    /**
     * <p>不带模板输出数据到Excel，数据传输类属性用 @ExcelProperty("") 标注</p >
     *
     * @param response 响应对象
     * @param tClass   输出格式
     * @param datas    输出的数据
     * @date 2021/9/2 0:32
     * @return：
     */
    public static <T> void writeExcel(HttpServletResponse response, Class<T> tClass, List<T> datas, String fileName) throws IOException, IllegalAccessException, NoSuchFieldException {
        if (response==null || tClass==null) {
            return;
        }

        if (StringUtils.isBlank(fileName)) {
            fileName = "excel.xlsx";
        } else {
            if (!fileName.contains(".xlsx")) {
                fileName = fileName + ".xlsx";
            }
        }

        //编码设置成UTF-8，excel文件格式为.xlsx
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 和easyexcel本身没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);

        //处理注解转换
//        if(CollUtil.isNotEmpty(datas)){
//            for (T dataObj : datas) {
//                AnnotationUtil.resolveAnnotation(tClass, dataObj);
//            }
//        }
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), tClass).build();
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName("sheet");
        excelWriter.write(datas, writeSheet);
        excelWriter.finish();
    }

}